(function($, doc) {
	$.init({});		
	
	$.plusReady(function() {
		document.getElementById('verify').src=apiurl+'Login/vertify?';
		//点击随机二维码
		document.getElementById("verify").addEventListener('tap', function() {
			document.getElementById('verify').src=apiurl+'Login/vertify?'+Math.random();
		});
		
		var regButton = doc.getElementById('reg');
		var inviteBox = doc.getElementById('invite');
		var nicknameBox = doc.getElementById('nickname');
		var phoneBox = doc.getElementById('phone');
		var passwordBox = doc.getElementById('login_pwd');
		var tradePasswordBox = doc.getElementById('trade_pwd');
		var codeBox = doc.getElementById('code');
		var random_codeBox=doc.getElementById('random_code');
		var num = /^\d{6}$/;
		var pwd_match=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,32}$/;
       
		regButton.addEventListener('tap', function(){
			var area_code=doc.getElementById('area_code').value;
			var loginInfo = {
				pid:  inviteBox.value,
				nickname:  nicknameBox.value,
				phone:    phoneBox.value,
				code: codeBox.value,
				password: passwordBox.value.trim(),
				trade_password: tradePasswordBox.value.trim(),
				country: doc.getElementById('country').value,
				area_code: doc.getElementById('area_code').value,
				random_code: random_codeBox.value
			};

			if(loginInfo.pid=='') {
				mui.alert('请输入邀请码');
				return;
			}
			
			if(loginInfo.nickname=='') {
				mui.alert('请输入昵称');
				return;
			}
			
			if(loginInfo.phone=='') {
				mui.alert('请输入手机号码');
				return;
			}
			if(!(/^1(3|4|5|6|7|8)\d{9}$/.test(loginInfo.phone)) && area_code=='86') {
				mui.alert("手机号码格式有误，请重填手机号");
				return ;
			}
			
			if(!pwd_match.exec(loginInfo.password)){
           		  mui.alert('登录密码为6~32位数字加字母！');
           		  return false;
           	 }
			
			if(loginInfo.trade_password=='') {
				mui.alert('请输入支付密码');
				return;
			}
			
			if(!num.exec(loginInfo.trade_password)){
           		  mui.alert('支付密码只能为6位纯数字！');
           		  return false;
           	 }

			if(loginInfo.random_code=='') {
				mui.alert('请输入随机码');
				return;
			}
			
            if(loginInfo.code=='') {
				mui.alert('请输入验证码');
				return;
			}
            
            console.log(JSON.stringify(loginInfo));
			mui.ajax({
				url: regurl,
				data: loginInfo,
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					// 请求成功
					if(res.success < 1) {
						mui.toast(res.msg);
						document.getElementById('verify').src=apiurl+'Login/vertify?'+Math.random();
					} else {
						mui.toast(res.msg);
						mui.back();
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('注册失败！');
				}
			});
			
		}, false);

		doc.getElementById('trade_pwd').addEventListener('input', function(){
			var p=this.value.trim();
			var len=p.length;
			var la=p.substr(len-1,1);
			if(isNaN(la)){
				mui.alert('支付密码为纯数字');
				this.value=p.substr(0,len-1);
				return ;
			}
			if(p.length>6){
				this.value=p.substr(0,6);
			}
		}, false);
		
		doc.getElementById('login_pwd').addEventListener('input', function(){
			var p=this.value.trim();
			var len=p.length;
			if(p.length>32){
				this.value=p.substr(0,32);
			}
		}, false);
		
		
		//点击国家/地区
		document.getElementById("nation").addEventListener('tap', function() {
			mui.openWindow({
				id: 'nation_list.html',
				url: 'nation_list.html'
			});
		});
	
		//添加自定义事件监听
		window.addEventListener('update_code', function(event) {
			//获得事件参数
			var country = event.detail.name;
			var area_code = event.detail.code;
			//console.log(area_code);
			document.getElementById('nation').innerHTML ='+'+ area_code ;
			document.getElementById('country').value = country;
			document.getElementById('area_code').value = area_code;
		});
	
	});
}(mui, document));